package co.baiku.boot.common.exception;

import co.baiku.boot.common.message.ErrorCode;
import org.springframework.http.HttpStatus;

/**
 * API异常
 */
public class ApiException extends RuntimeException {
    /**
     * 响应代码
     */
    private HttpStatus httpStatus;
    /**
     * 业务代码
     */
    private int code;
    /**
     * 额外数据
     */
    private Object extra;

    public static ApiException of(String message) {
        return new ApiException(message);
    }

    public static ApiException of(String message, Object extra) {
        return new ApiException(HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST.value(), message, extra);
    }

    public static ApiException of(String message, int code, Object extra) {
        return new ApiException(HttpStatus.BAD_REQUEST, code, message, extra);
    }

    public static ApiException of(ErrorCode error) {
        return of(HttpStatus.BAD_REQUEST, error.getCode(), error.getMsg());
    }

    public static ApiException of(ErrorCode error, Object extra) {
        return of(HttpStatus.BAD_REQUEST, error.getCode(), error.getMsg(), extra);
    }

    public static ApiException of(HttpStatus httpStatus, String message) {
        return new ApiException(httpStatus, message);
    }

    public static ApiException of(HttpStatus httpStatus, int code, String message) {
        return new ApiException(httpStatus, code, message);
    }

    public static ApiException of(HttpStatus httpStatus, int code, String message, Object extra) {
        return new ApiException(httpStatus, code, message, extra);
    }

    public ApiException(String message) {
        this(HttpStatus.BAD_REQUEST, message);
    }

    public ApiException(HttpStatus httpStatus, String message) {
        this(httpStatus, httpStatus.value(), message);
    }

    public ApiException(HttpStatus httpStatus, int code, String message) {
        this(httpStatus, code, message, null);
    }

    public ApiException(HttpStatus httpStatus, int code, String message, Object extra) {
        super(message);
        this.code = code;
        this.extra = extra;
        this.httpStatus = httpStatus;
    }

    public Object getExtra() {
        return extra;
    }

    public void setExtra(Object extra) {
        this.extra = extra;
    }

    public HttpStatus getHttpStatus() {
        return httpStatus;
    }

    public void setHttpStatus(HttpStatus httpStatus) {
        this.httpStatus = httpStatus;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}
